using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Types;

namespace Storage
{
    using Exceptions;
    using EntityModels;

    #region StorageManager
   
    /// <summary>
    /// Rapresents the way in which the modules can handle and persist their own data.
    /// </summary>
    public sealed partial class StorageManager
    {
        #region Public methods

        #region GetAllStagesByTripId

        /// <summary>
        /// Gets the stages that belongs to a certain trip.
        /// </summary>
        /// <param name="tripId">The id of the trip.</param>
        /// <returns>A List of Stage.</returns>
        public List<Stage> GetAllStagesByTripId(int tripId)
        {
            return GetTripById(tripId)
                .Stages
                .OrderBy(s => s.Ordinal)
                .ToList();
        }

        #endregion

        #region GetStagesWithReservationRequestByUserId

        /// <summary>
        /// Returns the id's of all stages with a request of a seat reservation for a certain user.
        /// </summary>
        /// <param name="userId">The id of the user.</param>
        /// <returns>A List of int.</returns>
        public List<int> GetStagesWithReservationRequestByUserId(int userId)
        {
            // Check if the user exists.
            User user = GetUserById(userId);

            var reservedStages = from request in Entities.ReservationRequests
                                 where request.User == user
                                 from stage in request.Trip.Stages
                                 select stage.Id;

            return reservedStages.ToList();
        }

        #endregion

        #region GetReservedStageListByUserId

        /// <summary>
        /// Returns the id's of all stages with a confirmation for a seat reservation
        /// for a certain user.
        /// </summary>
        /// <returns>A List of int.</returns>
        public List<int> GetReservedStagesByUserId(int userId)
        {
            // Check if the user exists.
            User user = GetUserById(userId);

            var stages = from stage in Entities.Users_Stages
                         where stage.User == user
                         select stage.StageId;

            return stages.ToList();
        }

        #endregion

        #endregion
    }
 
    #endregion
}
